﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace QQ2564874169.Core.ORM
{
    //todo 先加点常用的
    public class SetWhere<T> where T:new()
    {
        internal T eq;
        internal T neq;
        internal T less;
        internal T greater;
        internal T lessEq;
        internal T greaterEq;
        internal T like;
        internal List<T> _in;
        internal ChoiceField<T> asc;
        internal ChoiceField<T> desc;
        internal ChoiceField<T> max;
        internal ChoiceField<T> min;
        internal ChoiceField<T> sum;
        internal ChoiceField<T> count;
        internal ChoiceField<T> group;
        internal ChoiceField<T> fields;

        public SetWhere<T> Clone()
        {
            return new SetWhere<T>
            {
                eq = eq,
                neq = neq,
                less = less,
                greater = greater,
                lessEq = lessEq,
                greaterEq = greaterEq,
                like = like,
                _in = _in,
                asc = asc,
                desc = desc,
                max = max,
                min = min,
                sum = sum,
                count = count,
                group = group,
                fields = fields,
                Take = Take,
                Skip = Skip,
                ForUpdate = ForUpdate
            };
        }

        public ChoiceField<T> Fields
        {
            get
            {
                if (fields == null)
                {
                    fields = new ChoiceField<T>();
                }

                return fields;
            }
        }

        public ChoiceField<T> Group
        {
            get
            {
                if (group == null)
                {
                    group = new ChoiceField<T>();
                }

                return group;
            }
        }

        public ChoiceField<T> Count
        {
            get
            {
                if (count == null)
                {
                    count = new ChoiceField<T>();
                }

                return count;
            }
        }

        public ChoiceField<T> Sum
        {
            get
            {
                if (sum == null)
                {
                    sum = new ChoiceField<T>();
                }

                return sum;
            }
        }

        public ChoiceField<T> Min
        {
            get
            {
                if (min == null)
                {
                    min = new ChoiceField<T>();
                }

                return min;
            }
        }

        public ChoiceField<T> Max
        {
            get
            {
                if (max == null)
                {
                    max = new ChoiceField<T>();
                }

                return max;
            }
        }

        public ChoiceField<T> Desc
        {
            get
            {
                if (desc == null)
                {
                    desc = new ChoiceField<T>();
                }

                return desc;
            }
        }

        public ChoiceField<T> Asc
        {
            get
            {
                if (asc == null)
                {
                    asc = new ChoiceField<T>();
                }

                return asc;
            }
        }

        public T Eq
        {
            get
            {
                if (eq == null)
                {
                    eq = Activator.CreateInstance<T>();
                }

                return eq;
            }
        }

        public T Neq
        {
            get
            {
                if (neq == null)
                {
                    neq = Activator.CreateInstance<T>();
                }

                return neq;
            }
        }
        public T Less
        {
            get
            {
                if (less == null)
                {
                    less = Activator.CreateInstance<T>();
                }

                return less;
            }
        }
        public T LessEq
        {
            get
            {
                if (lessEq == null)
                {
                    lessEq = Activator.CreateInstance<T>();
                }

                return lessEq;
            }
        }
        public T Like
        {
            get
            {
                if (like == null)
                {
                    like = Activator.CreateInstance<T>();
                }

                return like;
            }
        }
        public T Greater
        {
            get
            {
                if (greater == null)
                {
                    greater = Activator.CreateInstance<T>();
                }

                return greater;
            }
        }
        public T GreaterEq
        {
            get
            {
                if (greaterEq == null)
                {
                    greaterEq = Activator.CreateInstance<T>();
                }

                return greaterEq;
            }
        }
        public List<T> In
        {
            get
            {
                if (_in == null)
                {
                    _in = new List<T>();
                }

                return _in;
            }
        }

        public void SetIn<N>(IEnumerable<N> items, Action<T, N> set)
        {
            foreach (var item in items)
            {
                var model = Activator.CreateInstance<T>();
                set(model, item);
                In.Add(model);
            }
        }

        public bool ForUpdate { get; set; }
        public int? Skip { get; set; }
        public int? Take { get; set; }
    }
}
